package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.PurchaseArrivalMapper;
import com.spark.incoicings.dao.PurchaseDetailsMapper;
import com.spark.incoicings.dao.ReceiptDetailMapper;
import com.spark.incoicings.service.PurchaseArrivalService;
import com.spark.invoicings.entity.PurchaseArrival;
import com.spark.invoicings.entity.PurchaseDetail;
import com.spark.invoicings.entity.ReceiptDetail;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PurchaseArrivalServiceImpl
        extends ServiceImpl<PurchaseArrivalMapper, PurchaseArrival>
        implements PurchaseArrivalService {

    @Resource
    private PurchaseArrivalMapper purchaseArrivalMapper;

    @Resource
    private ReceiptDetailMapper receiptDetailMapper;

    @Resource
    private PurchaseDetailsMapper purchaseDetailMapper;

    /**
     * 根据id获取采购到货单
     * @param id
     * @return
     */
    @Override
    public PurchaseArrival selectPurchaseArrivalById(Integer id) {
        PurchaseArrival purchaseArrival = purchaseArrivalMapper.selectPurchaseArrivalById(id);
        List<ReceiptDetail> receiptDetails = receiptDetailMapper.selectReceiptDetailById(purchaseArrival.getArrivalId());
        purchaseArrival.setReceiptDetails(receiptDetails);
        return purchaseArrival;
    }

    /**
     * 根据id删除采购到货单
     * @param id
     * @return
     */
    @Override
    public boolean delPurchaseArrival(Integer id) {
        // 判断该到货单下面是否有到货明细单
        List<ReceiptDetail> receiptDetails = receiptDetailMapper.selectReceiptDetailById(id);
        if (receiptDetails == null || receiptDetails.isEmpty()) {
            return false;
        }
        return purchaseArrivalMapper.deleteById(id) > 0;
    }

    @Override
    public Result updatePurchaseArrivalByOId(PurchaseArrival purchaseArrival) {
        UpdateWrapper<PurchaseArrival> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("purchase_order_id", purchaseArrival.getPurchaseOrderId());
        purchaseArrivalMapper.update(purchaseArrival, updateWrapper);
        return Result.success(200, "success", purchaseArrival);
    }

    /**
     * 根据id更新采购到货单
     * @param purchaseArrival
     * @return
     */
    @Override
    public Result updatePurchaseArrival(PurchaseArrival purchaseArrival) {
        for (ReceiptDetail receiptDetail : purchaseArrival.getReceiptDetails()) {
            // 根据采购明细id获取待到货数量
            PurchaseDetail purchaseDetail = purchaseDetailMapper.selectById(receiptDetail.getPurchaseDetailId());
            // 判断待到货单的待到货数量 是否超出采购订单明细的待到货数量
            if (receiptDetail.getReceivedQuantity() > purchaseDetail.getPendingArrivalQuantity()) {
                return Result.error(500,"超出采购订单明细的待到货数量");
            }
            receiptDetailMapper.updateById(receiptDetail);
        }
        return Result.success(200, "success", purchaseArrival.getArrivalId());
    }

    /**
     * 添加采购到货单
     * @param purchaseArrival
     * @return
     */
    @Transactional
    @Override
    public Result addPurchaseArrival(PurchaseArrival purchaseArrival) {
        for (ReceiptDetail receiptDetail : purchaseArrival.getReceiptDetails()) {
            // 根据采购明细id获取待到货数量
            PurchaseDetail purchaseDetail = purchaseDetailMapper.selectById(receiptDetail.getPurchaseDetailId());
            // 判断待到货单的待到货数量 是否超出采购订单明细的待到货数量
            if (receiptDetail.getReceivedQuantity() > purchaseDetail.getPendingArrivalQuantity()) {
                return Result.error(500,"超出采购订单明细的待到货数量");
            }
            receiptDetail.setReceiptOrderId(purchaseArrival.getArrivalId());
            receiptDetailMapper.insert(receiptDetail);
        }
        purchaseArrivalMapper.insert(purchaseArrival);
        for (ReceiptDetail receiptDetail : purchaseArrival.getReceiptDetails()) {
            receiptDetail.setReceiptOrderId(purchaseArrival.getArrivalId());
            receiptDetailMapper.updateById(receiptDetail);
        }
        return Result.success(200, "success", purchaseArrival.getArrivalId());
    }

    /**
     * 查询所有采购到货单
     * @param map
     * @return
     */
    @Override
    public List<PurchaseArrival> selectPurchaseArrivalAll(Map<String, Object> map) {
        return purchaseArrivalMapper.selectPurchaseArrivalAll(map);
    }
}
